package weaver.formmode.customjavacode.modeexpand.srm.cg;

import com.engine.cese.duss.srm.util.CalUtil;
import com.engine.cese.duss.srm.util.GenUtil;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.customjavacode.modeexpand.srm.util.SrmUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import javax.security.sasl.SaslServer;
import java.util.HashMap;
import java.util.Map;


/**
 * @Description: 物料类采购任务池-任务合并
 * @Param:
 * @return:
 * @Author: Dusongsong
 * @Date: 2021/3/23
 */
public class WeaWlTaskMergeNewSaveAction extends AbstractModeExpandJavaCodeNew {

    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User) param.get( "user" );
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                if (billid > 0 && modeid > 0) {
                    //------请在下面编写业务逻辑代码------
                    BaseBean baseBean = new BaseBean();
                    RecordSet rs = new RecordSet();
                    String cglx = "0";
                    String cgrwTable = SrmUtil.getCgrwTableType( cglx );
                    String rwhbTable = SrmUtil.getRwhbTableType( cglx );
                    String zbcglx = baseBean.getPropValue( "SrmWfTab", "zbcglx" );
                    String xxcglx = baseBean.getPropValue( "SrmWfTab", "xxcglx" );
                    String xjcglx = baseBean.getPropValue( "SrmWfTab", "xjcglx" );
                    baseBean.writeLog( "*****************billid**********" + billid );
                    //将所选任务罗列至《物料类任务合并》的明细中，选择目标《立项单》进行合并
                    //根据“项目名称”+“需求包号”进行唯一标识，
                    //①如果相同的，“物料编号”相同的，数量合并；“物料编号”不同的，新增行；
                    //②如不同的，直接将该需求包，插入《立项单》的明细中；
                    //任务合并建模明细
                    String sql = "select lxd,rwid from " + rwhbTable + " where id=?";
                    rs.executeQuery( sql, billid );
                    baseBean.writeLog( sql + "," + billid );
                    rs.next();
                    String hblxd = Util.null2String( rs.getString( "lxd" ) );//立项单
                    String hbrwid = Util.null2String( rs.getString( "rwid" ) );//任务单
                    String sql11 ="select wm_concat(tab) from (\n" +
                            "select decode(count(1),'0','','1','"+xxcglx+"') as tab from "+xxcglx+" where requestid=?\n" +
                            "union all\n" +
                            "select decode(count(1),'0','','1','"+zbcglx+"') as tab from "+zbcglx+" where requestid=?\n" +
                            "union all\n" +
                            "select decode(count(1),'0','','1','"+xjcglx+"') as tab from "+xjcglx+" where requestid=?\n" +
                            ")";
                    rs.executeQuery(sql11,hblxd,hblxd,hblxd);
                    rs.next();
                    String formtable = Util.null2String(rs.getString( 1 ));
                    String mainid = SrmUtil.getRequestid2Id( hblxd,formtable);
                    updateRwdid(hblxd,hbrwid,formtable);
                    String sql1 = "select xqdw,xqdwmc,cgqx,xmmc,xmbh,shdd,wllb,ppfw,spdhrq,\n" +
                            "ygje,bmid,fbid,xqbh,rwdid from " + rwhbTable + "_dt1 where mainid=?";
                    rs.executeQuery( sql1, billid );
                    baseBean.writeLog( sql1 + "," + billid );
                    while (rs.next()) {
                        String xqdw = Util.null2String( rs.getString( "xqdw" ) );//需求单位
                        String xqdwmc = Util.null2String( rs.getString( "xqdwmc" ) );//需求单位名称
                        String cgqx = Util.null2String( rs.getString( "cgqx" ) );//采购权限
                        String xmmc = Util.null2String( rs.getString( "xmmc" ) );//项目名称
                        String xmbh = Util.null2String( rs.getString( "xmbh" ) );//项目编号
                        String shdd = Util.null2String( rs.getString( "shdd" ) );//收货地点
                        String wllb = Util.null2String( rs.getString( "wllb" ) );//物料类别
                        String ppfw = Util.null2String( rs.getString( "ppfw" ) );//品牌范围
                        String spdhrq = Util.null2String( rs.getString( "spdhrq" ) );//首批到货日期
                        String ygje = Util.null2String( rs.getString( "ygje" ) );//预估金额
                        String bmid = Util.null2String( rs.getString( "bmid" ) );//部门id
                        String fbid = Util.null2String( rs.getString( "fbid" ) );//分部Id
                        String xqbh = Util.null2String( rs.getString( "xqbh" ) );//需求包号
                        String hbdmxrwdid = Util.null2String( rs.getString( "rwdid" ) );//任务单ID
                        String hbdwybs = xmmc + xqbh;
                        //立项单流程明细
                        String lxdrwdids = getExitsTask( hblxd, hbdwybs,formtable,ygje);
                        baseBean.writeLog( "lxdrwdids=" + lxdrwdids );
                        if (!"".equals( lxdrwdids )) {//存在合并项
                            getMergeTaskData( hbdmxrwdid, lxdrwdids, cglx );
                        } else {//不存在合并项
                            String sql2 = "insert into " + formtable + "_dt2(mainid,xqdw,xqdwmc,cgqx,xmmc,xmbh,shdd,wllb,ppfw," +
                                    "spdhrq,ygje,bmid,fbid,xqbh,rwdid)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                            Object[] params = {mainid, xqdw, xqdwmc, cgqx, xmmc, xmbh, shdd, wllb, ppfw, spdhrq, ygje, bmid, fbid, xqbh, hbdmxrwdid};
                            rs.executeUpdate( sql2, params );
                            baseBean.writeLog( sql2 + "," + params.toString() );
                        }
                        //合并完成后更新任务状态
                        rs.executeUpdate( "update " + cgrwTable + " set rwzt=1 where id=?", hbdmxrwdid );
                        baseBean.writeLog("update " + cgrwTable + " set rwzt=1 where id=?", hbdmxrwdid );
                        SrmUtil.getTotalAmount(hbrwid,hblxd,cgrwTable,formtable);
                    }
                }
            }
        } catch (
                Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }

    //任务合并
    public static void getMergeTaskData(String hbdmxrwdid, String lxdrwdid, String cglx) {
        RecordSet rs = new RecordSet();
        String ids = hbdmxrwdid + "," + lxdrwdid;
        String cgrwTab = GenUtil.getCgrwTableType( cglx );
        rs.writeLog( "----------------getMergeTaskData---------" + ids );
        String[] arr = lxdrwdid.split( "," );
        for (int i = 0; i < arr.length; i++) {
            String mainid = arr[i];
            rs.executeQuery( "select wm_concat(id) as mxid from uf_cgrw_wl_dt1 where mainid =?", mainid );
            rs.next();
            String mxid = Util.null2String( rs.getString( "mxid" ) );
            mxid = mxid == "" ? "''" : mxid;
            String sql = "select wm_concat(xqmxid) as xqmxid,wlbm,wlmc," +
                    "cms,xlh,xlbz,dw,sum(sl) as sl,fzdw,sum(fzsl) as fzsl,\n" +
                    "sum(cgsl) as cgsl,sum(sysl) as sysl,fbbh,zfbbh,zfbid,wlid\n" +
                    "from " + cgrwTab + "_dt1 where mainid in(" + ids + ")\n" +
                    "group by wlbm,wlmc,cms,xlh,xlbz,dw,fzdw,fbbh,zfbbh,zfbid,wlid";
            rs.executeQuery( sql );
            while (rs.next()) {
                String wlbm = Util.null2String( rs.getString( "wlbm" ) );//物料编码
                String wlmc = Util.null2String( rs.getString( "wlmc" ) );//物料名称
                String cms = Util.null2String( rs.getString( "cms" ) );//长描述
                String xlh = Util.null2String( rs.getString( "xlh" ) );//序列号
                String xlbz = Util.null2String( rs.getString( "xlbz" ) );//序列备注
                String dw = Util.null2String( rs.getString( "dw" ) );//单位
                String sl = Util.null2String( rs.getString( "sl" ) );//数量
                String fzdw = Util.null2String( rs.getString( "fzdw" ) );//辅助单位
                String fzsl = Util.null2String( rs.getString( "fzsl" ) );//辅助数量
                String cgsl = Util.null2String( rs.getString( "cgsl" ) );//采购数量
                String sysl = Util.null2String( rs.getString( "sysl" ) );//剩余数量
                String fbbh = Util.null2String( rs.getString( "fbbh" ) );//分部编号
                String zfbbh = Util.null2String( rs.getString( "zfbbh" ) );//子分部编号
                String zfbid = Util.null2String( rs.getString( "zfbid" ) );//子分部id
                String wlid = Util.null2String( rs.getString( "wlid" ) );//物料id
                String xqmxid = Util.null2String( rs.getString( "xqmxid" ) );//需求明细id
                String sql1 = "insert into " + cgrwTab + "_dt1(mainid,wlbm,wlmc,cms,xlh,xlbz,dw,sl,fzdw," +
                        "fzsl,cgsl,sysl,fbbh,zfbbh,zfbid,wlid,xqmxid) " +
                        "values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                Object[] params = {mainid, wlbm, wlmc, cms, xlh, xlbz, dw, sl, fzdw,
                        fzsl, cgsl, sysl, fbbh, zfbbh, zfbid, wlid, xqmxid};
                rs.executeUpdate( sql1, params );
            }
            rs.executeUpdate( "delete from " + cgrwTab + "_dt1 where id in (" + mxid + ")" );
            rs.writeLog( "delete from " + cgrwTab + "_dt1 where id in (" + mxid + ")" );
        }
    }

    //判断是否存在任务
    public static String getExitsTask(String hblxd, String hbdwybs,String formtable,String amount) {
        RecordSet rs = new RecordSet();
        String lxdrwdids = "";
        String sql2 = "select t1.id,t1.rwdid,t1.ygje from " + formtable + " t left join "
                + formtable + "_dt2 t1 on t.id=t1.mainid where t.requestid=? and concat(t1.xmmc,t1.xqbh)=? ";
        rs.executeQuery( sql2, hblxd, hbdwybs );
        rs.writeLog( sql2 + "," + hblxd + "," + hbdwybs );
        if (rs.next()) {
            String mxid = Util.null2String( rs.getString( "id" ) );//明细ID
            lxdrwdids = Util.null2String( rs.getString( "rwdid" ) );//立项单任务单ID
            String ygje = Util.null2String( rs.getString( "ygje" ) );//立项单任务单ID
            String newamount = CalUtil.fAdd(ygje,amount);
            rs.executeUpdate("update "+formtable+"_dt2 set ygje=? where id=?",newamount,mxid);
        }
        return lxdrwdids;
    }
    //更新主表任务ID
    public static String updateRwdid(String hblxd,String hbrwid,String formtable){
        RecordSet rs = new RecordSet();
        String newRwdids ="";
        rs.executeQuery("select rwdids from "+formtable+" where requestid=?",hblxd);
        rs.next();
        String rwdids =Util.null2String(rs.getString("rwdids"));
        newRwdids = "".equals(rwdids)?hbrwid:rwdids+","+hbrwid;
        rs.executeUpdate("update "+formtable+" set rwdids=? where requestid=?",newRwdids,hblxd);
        return newRwdids;
    }
}